01. Introduction to Variables

Introduction

Solving Problems with Variables

As human beings, we recall and use information from our own experiences to solve problems. The same is true when we are programming—we also must recall and use information, or data, in order to build applications. As developers, we do this by using variables.

Consider the following equation:

theTipAmount = thePriceOfDinner * theTipPercentage

This equation calculates a tip (gratuity) for a meal. A tip is an amount of money paid in addition to the normal price of a service or good. Typically, the money paid as a tip goes directly to service sector workers for a service performed or anticipated. Note: In certain regions of the world, tipping is not customary; however, for this example let's assume it is.

If you read this moving from left-to-right, it says the tip amount is determined by multiplying the price of dinner by a tip percentage. Each of the names, or identifiers, used in this expression—theTipAmount, thePriceOfDinner, and theTipPercentage—are variables! And, we can associate values with them that allow us to solve problems.

A value can be substituted for each variable.

A value can be substituted for each variable.

For example, let’s say the price of dinner was $27.53 with tax included. And, because our wait staff was polite, we are going to tip 15%. So, as a decimal value, we’ll use 0.15 to represent 15%. Then, we can determine the tip amount which is 4.1295… hmm, let’s round that up to $4.13!

Variables Can Be Reused

Another nice benefit of variables is that they allow us to reuse expressions and formulas like calculating the tip multiple times, with multiple values.

Here we reuse the same variables to calculate a different tip amount.

Here we reuse the same variables to calculate a different tip amount.

So this time the price of dinner is $46.32 and we tip 20% instead 15%. Therefore, the total tip amount is 9.264 or $9.27 if we round up.

This is why we call them variables—they have the ability to vary their value.

Variable Have a Type

Another important aspect of variables is data type or “type” for short. A data type refers to the kind of data associated with a variable. For example, imagine a variable called myFavoriteNumber. Based on this variable’s name, we can probably assume that it contains a value that is a number and not something else like a photo. So as programmers, we’d say that the type of myFavoriteNumber is a number.

But, programming languages are not always so smart. Sometimes they can infer a variable’s type like we can, but in other cases we have to explicitly state a variable’s type. Here are examples from different programming languages where the type of a variable is explicitly stated as an Int which is short for Integer. An Integer is another word for a whole number.

Swift Java x86 Assembly
var myNumber: Int int myNumber; myNumber DD ? ; myNumber is an Int*

Basic Data Types

To make our lives easier, programming languages often specify built-in (also known as primitive) data types that we can use for our variables. Here are some of the primitive types in Swift:

Type Description Example Values
Int whole number values 0, 2, -2, 100, …
Float floating-point (decimal) numbers 3.14, 5.693, -12.321, …
Double floating-point (decimal) numbers that have even more precision—use this instead of Float 3.14, 5.693, -12.321, …
Character a single letter, digit, symbol, or code "a", "+", "q", …
String combinations of characters “swift”, “I Love Swift”, “ ”, “:)”, …
Bool a truth value true, false

Note: This is not an exhaustive list of the primitive types in Swift.

Reflect: Objects in the Maze App

QUESTION:

Now that you've seen some basic data types, you should try imagining a variable that could represent each one. Come up with a variable that represents an Int, a Float, a Double, a Character, a String, and a Bool. For example, a variable called myHourlyWage could be a Float variable.

ANSWER:

Well done. If desired, consider sharing your variable names on the forums.